(function() {
    'use strict';

    angular
        .module('zhserverApp')
        .controller('RuleRepositoryNoDialogController', RuleRepositoryNoDialogController);

    RuleRepositoryNoDialogController.$inject = ['$timeout', '$scope', '$stateParams',  'entity', 'RuleRepository','$state'];

    function RuleRepositoryNoDialogController ($timeout, $scope, $stateParams,  entity, RuleRepository,$state) {
        var vm = this;

        vm.ruleRepository = entity;
        vm.clear = clear;
        vm.save = save;

        $timeout(function (){
            angular.element('.form-group:eq(1)>input').focus();
        });

        function clear () {
            $state.go('zhRule-repositorymySuffix', null, { reload: 'zhRule-repositorymySuffix' });

        }

        function save () {
            vm.isSaving = true;
            if (vm.ruleRepository.id !== null) {
                RuleRepository.update(vm.ruleRepository, onSaveSuccess, onSaveError);
                $state.go('zhRule-repositorymySuffix', null, { reload: 'zhRule-repositorymySuffix' });

            } else {
                RuleRepository.save(vm.ruleRepository, onSaveSuccess, onSaveError);
                $state.go('zhRule-repositorymySuffix', null, { reload: 'zhRule-repositorymySuffix' });

            }
        }

        function onSaveSuccess (result) {
            $scope.$emit('zhserverApp:ruleRepositoryUpdate', result);
            vm.isSaving = false;
        }

        function onSaveError () {
            vm.isSaving = false;
        }


    }
})();
